var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');



var app = getApp();

Page({
  data: {
    switchDatabaseArr: [],
    switchDatabaseId: [],
    index: 0
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    });
  },
  sbmitSwitchDatabase : function(e){
    let that = this;
    
    if (that.data.index == 0){
      util.showErrorToast('请选择要切换的数据库');
      return false;
    }

    wx.showLoading({
      title: '提交中...',
      mask:true,
      success: function () {

      }
    });

    let let_datasourceId = that.data.switchDatabaseId[that.data.index];
    util.request(api.SwitchDatabaseSave, { datasourceId: let_datasourceId }).then(function (res) {
      if (res.errno === 0) {

        wx.hideLoading();

        wx.showToast({
          title: res.data,
          icon: 'success',
          duration: 2000,
          complete: function () {
            that.setData({
              switchDatabaseArr: [],
              switchDatabaseId: [],
              index: 0
            });
          }
        });
      } else {
        util.showErrorToast(res.data);
      }
      
    });
  },

  getSwitchDatabaseList(){
    let that = this;
    util.request(api.SwitchDatabaseList).then(function (res) {
      if (res.errno === 0) {
        let qArr = [];
        let qId = [];
        qArr.push('请选择');
        qId.push('0');

        for (var i = 0; i < res.data.length; i++) {
          qArr.push(res.data[i].dbName+" (数据库编号:"+res.data[i].dbId+")")
          qId.push(res.data[i].dbId)
        }

        that.setData({
          switchDatabaseArr: qArr,
          switchDatabaseId: qId
        });
      }
    });
  },

  onLoad: function (options) {
  },
  onReady: function () {

  },
  onShow: function () {
    this.getSwitchDatabaseList();
  },
  
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  }
})